﻿using AutoMapper;
using SIG.Model.Store;
using SIG.Service;
using SIG.ViewModel.Front.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace SIG.WebMVC.Controllers.API
{
    public class GoodsController : BaseApiController
    {
        // GET: Goods
        private readonly IGoodsCategoryService _goodsCategoryService;
        private readonly IGoodsService _goodsService;
        private readonly IPageMetaService _pageMetaService;
        private IMapper _mapper;
        public GoodsController(IGoodsService goodsService, IGoodsCategoryService goodsCategoryService, IPageMetaService pageMetaService, IMapper mapper)
        {
            _goodsService = goodsService;
            _goodsCategoryService = goodsCategoryService;
            _pageMetaService = pageMetaService;
            _mapper = mapper;
        }

        [HttpGet]
        [Route("api/goods/list")]

        public async Task<HttpResponseMessage> List()
        {
            try
            {
                var goodsList = await _goodsService.GetActiveElementsAync();
                var goods = _mapper.Map<IList<Goods>, IList<GoodsVM>>(goodsList);
                return Request.CreateResponse(HttpStatusCode.OK, goods);
            }
            catch (Exception ex)
            {
                HttpError err = new HttpError(ex.Message);
                return Request.CreateResponse(HttpStatusCode.BadRequest, err);
            }

        }
    }
}
